package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_result;
    private String firstNum = ""; // 记录第一个操作数
    private String secondNum = ""; // 记录第二个操作数
    private String operator = ""; // 记录运算符
    private String result = ""; // 记录运算结果
    private String showText = ""; // 记录显示文本

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        tv_result = findViewById(R.id.tv_result);
        // 给每个按钮控件注册点击监听器
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_plus).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);
        findViewById(R.id.btn_multiply).setOnClickListener(this);
        findViewById(R.id.btn_reciprocal).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
        findViewById(R.id.ib_sqrt).setOnClickListener(this);
        findViewById(R.id.btn_divide).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        String inputText;
        if (v.getId() == R.id.ib_sqrt) {
            inputText = "√";
        } else {
            inputText = ((TextView) v).getText().toString();
        }
        switch (v.getId()) {
            // 清除
            case R.id.btn_clear:
                clear();
                break;
            case R.id.btn_cancel:
                if (!showText.equals("")) {
                    showText = showText.substring(0, showText.length() - 1);
                    if (operator.equals("")) {
                        firstNum = showText;
                    } else {
                        secondNum = showText;
                    }
                    refreshResult(showText);
                }
                break;
            case R.id.btn_plus:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                if (firstNum.equals("")) {
                    firstNum = "0";
                }
                operator = inputText;
                refreshResult(showText + inputText);
                break;
            case R.id.btn_equal:
                double calculate_result = calculateFour();
                if (calculate_result == 0) {
                    result = showText;
                } else {
                    refreshOperate(String.valueOf(calculate_result));
                    refreshResult(showText + "=" + result);
                }

                break;
            case R.id.ib_sqrt:
                if (operator.equals("")) {
                    double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
                    refreshOperate(String.valueOf(sqrt_result));
                    refreshResult("√" + firstNum + "=" + result);
                } else {
                    double sqrt_result = Math.sqrt(Double.parseDouble(secondNum));
                    secondNum = String.valueOf(sqrt_result);
                    refreshResult(firstNum + operator + secondNum);
                }
                break;
            case R.id.btn_reciprocal:
                double reciprocal_result = 1 / Double.parseDouble(firstNum);
                refreshOperate(String.valueOf(reciprocal_result));
                refreshResult("1/" + showText + "=" + result);
                break;
            default:
                // 数字键
                if ((result.contains("Error")) || (!result.equals("") && operator.equals(""))) {
                    clear();
                }
                if (operator.equals("")) {
                    firstNum += inputText;
                } else {
                    secondNum += inputText;
                }
                if (showText.equals("0") && !inputText.equals(".")) {
                    refreshResult(inputText);
                } else {
                    refreshResult(showText + inputText);
                }
                break;
        }

    }

    private double calculateFour() {
        if (secondNum.equals("")) {
            secondNum = "0";
        }
        switch (operator) {
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "x":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            case "÷":
                if (Double.parseDouble(secondNum) == 0) {
                    clear();
                    tv_result.setText("Error: 除数不能为0");
                    return 0;
                }
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
        return 0;
    }

    // 清除
    private void clear() {
        refreshOperate("");
        refreshResult("");
        firstNum = "";
        secondNum = "";
        operator = "";
        result = "";
        showText = "";
    }

    // 刷新运算结果
    private void refreshOperate(String new_result) {
        result = new_result;
        firstNum = result;
        secondNum = "";
        operator = "";
    }

    // 刷新文本显示
    private void refreshResult(String text) {
        showText = text;
        tv_result.setText(showText);
    }
}